// ElaStatusBarTest.cpp
#include "ElaStatusBarTest.h"
#include "ElaTheme.h"
#include <QApplication>
#include <QPushButton>
#include <QSpacerItem>
#include <QDateTime>

// addPermanentWidget() 添加永久项目（靠右显示）

ElaStatusBarTest::ElaStatusBarTest(QWidget* parent) : QWidget(parent), m_statusBar(nullptr), m_isConnected(false), m_batteryLevel(85), m_progressValue(0), m_isLightTheme(true)
{
    setupStatusBar();

    // 创建定时器用于演示动态更新
    m_progressTimer = new QTimer(this);
    connect(m_progressTimer, &QTimer::timeout, this, &ElaStatusBarTest::updateProgress);
    m_progressTimer->start(2000);  // 每2秒更新一次

    // 监听主题变化
    connect(eTheme, &ElaTheme::themeModeChanged, this, [this](ElaThemeType::ThemeMode mode) {
        m_isLightTheme = (mode == ElaThemeType::Light);
        updateThemeIndicator();
    });
}

ElaStatusBarTest::~ElaStatusBarTest()
{
}

void ElaStatusBarTest::setupStatusBar()
{
    // 创建状态栏
    m_statusBar = new ElaStatusBar(this);

    // 设置状态栏的基本属性
    m_statusBar->setFixedHeight(30);                // 设置固定高度
    m_statusBar->setSizeGripEnabled(true);          // 启用大小调整手柄
    m_statusBar->setContentsMargins(10, 2, 10, 2);  // 设置边距

    // 设置样式表（可选）
    m_statusBar->setStyleSheet(
        "QStatusBar {"
        "    border-top: 1px solid #cccccc;"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f8f8f8, stop:1 #e8e8e8);"
        "}");

    // 创建各种状态栏项目
    createConnectionStatusItem();
    createBatteryStatusItem();
    createMemoryUsageItem();
    createThemeIndicatorItem();
    createCustomMessageArea();

    // 添加分隔符
    m_statusBar->addPermanentWidget(new QLabel(" | "));

    // 添加时间显示
    QLabel* timeLabel = new QLabel(QDateTime::currentDateTime().toString("hh:mm:ss"), m_statusBar);
    timeLabel->setAlignment(Qt::AlignCenter);
    m_statusBar->addPermanentWidget(timeLabel);

    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    // 添加一些测试控件
    QWidget* testControls = new QWidget(this);
    QHBoxLayout* controlsLayout = new QHBoxLayout(testControls);

    QPushButton* toggleConnectionBtn = new QPushButton("切换连接状态", this);
    connect(toggleConnectionBtn, &QPushButton::clicked, this, &ElaStatusBarTest::toggleConnectionStatus);

    QPushButton* toggleThemeBtn = new QPushButton("切换主题指示器", this);
    connect(toggleThemeBtn, &QPushButton::clicked, this, &ElaStatusBarTest::toggleThemeIndicator);

    QPushButton* showMessageBtn = new QPushButton("显示消息", this);
    connect(showMessageBtn, &QPushButton::clicked, this, &ElaStatusBarTest::showCustomMessage);

    QPushButton* clearMessageBtn = new QPushButton("清除消息", this);
    connect(clearMessageBtn, &QPushButton::clicked, this, &ElaStatusBarTest::clearCustomMessage);

    controlsLayout->addWidget(toggleConnectionBtn);
    controlsLayout->addWidget(toggleThemeBtn);
    controlsLayout->addWidget(showMessageBtn);
    controlsLayout->addWidget(clearMessageBtn);
    controlsLayout->addStretch();

    // 添加说明文本
    ElaText* infoText = new ElaText(
        "这是一个ElaStatusBar的完整演示\n"
        "展示了状态栏的各种功能和属性设置",
        this);
    infoText->setTextStyle(ElaTextType::Body);
    infoText->setAlignment(Qt::AlignCenter);

    mainLayout->addWidget(infoText);
    mainLayout->addWidget(testControls);
    mainLayout->addStretch();
    mainLayout->addWidget(m_statusBar);
}

void ElaStatusBarTest::createConnectionStatusItem()
{
    m_connectionContainer = new QWidget(m_statusBar);
    QHBoxLayout* layout = new QHBoxLayout(m_connectionContainer);
    layout->setContentsMargins(8, 0, 8, 0);
    layout->setSpacing(4);

    m_connectionIcon = new ElaIconButton(ElaIconType::Wifi, 16, m_connectionContainer);
    m_connectionIcon->setEnabled(false);

    m_connectionText = new QLabel("离线", m_connectionContainer);
    m_connectionText->setStyleSheet("color: gray; font-size: 12px;");

    layout->addWidget(m_connectionIcon);
    layout->addWidget(m_connectionText);

    m_statusBar->addWidget(m_connectionContainer);
}

void ElaStatusBarTest::createBatteryStatusItem()
{
    m_batteryContainer = new QWidget(m_statusBar);
    QHBoxLayout* layout = new QHBoxLayout(m_batteryContainer);
    layout->setContentsMargins(8, 0, 8, 0);
    layout->setSpacing(4);

    m_batteryIcon = new ElaIconButton(ElaIconType::BatteryFull, 16, m_batteryContainer);

    m_batteryText = new QLabel("85%", m_batteryContainer);
    m_batteryText->setStyleSheet("font-size: 12px;");

    layout->addWidget(m_batteryIcon);
    layout->addWidget(m_batteryText);

    m_statusBar->addWidget(m_batteryContainer);
}

void ElaStatusBarTest::createMemoryUsageItem()
{
    m_memoryContainer = new QWidget(m_statusBar);
    QHBoxLayout* layout = new QHBoxLayout(m_memoryContainer);
    layout->setContentsMargins(8, 0, 8, 0);
    layout->setSpacing(4);

    QLabel* memoryIcon = new QLabel("💾", m_memoryContainer);
    memoryIcon->setStyleSheet("font-size: 12px;");

    m_memoryProgress = new ElaProgressBar(m_memoryContainer);
    m_memoryProgress->setFixedSize(60, 12);
    m_memoryProgress->setValue(45);

    m_memoryText = new QLabel("45%", m_memoryContainer);
    m_memoryText->setStyleSheet("font-size: 10px; color: gray;");

    layout->addWidget(memoryIcon);
    layout->addWidget(m_memoryProgress);
    layout->addWidget(m_memoryText);

    m_statusBar->addPermanentWidget(m_memoryContainer);
}

void ElaStatusBarTest::createThemeIndicatorItem()
{
    m_themeContainer = new QWidget(m_statusBar);
    QHBoxLayout* layout = new QHBoxLayout(m_themeContainer);
    layout->setContentsMargins(8, 0, 8, 0);
    layout->setSpacing(4);

    m_themeIcon = new ElaIconButton(ElaIconType::Sun, 16, m_themeContainer);
    m_themeIcon->setToolTip("当前主题: 浅色");

    layout->addWidget(m_themeIcon);

    m_statusBar->addPermanentWidget(m_themeContainer);
}

void ElaStatusBarTest::createCustomMessageArea()
{
    m_customMessage = new QLabel("", m_statusBar);
    m_customMessage->setStyleSheet("font-size: 12px; color: #0066cc; padding: 0 8px;");
    m_customMessage->setAlignment(Qt::AlignCenter);

    m_statusBar->addWidget(m_customMessage);
}

void ElaStatusBarTest::toggleConnectionStatus()
{
    m_isConnected = !m_isConnected;

    if (m_isConnected)
    {
        m_connectionIcon->setAwesome(ElaIconType::Wifi);
        m_connectionIcon->setEnabled(true);
        m_connectionText->setText("已连接");
        m_connectionText->setStyleSheet("color: green; font-size: 12px;");
        m_connectionIcon->setToolTip("网络连接正常");
    }
    else
    {
        m_connectionIcon->setAwesome(ElaIconType::WifiSlash);
        m_connectionIcon->setEnabled(false);
        m_connectionText->setText("离线");
        m_connectionText->setStyleSheet("color: gray; font-size: 12px;");
        m_connectionIcon->setToolTip("网络连接断开");
    }
}

void ElaStatusBarTest::updateProgress()
{
    // 模拟内存使用率变化
    static bool increasing = true;

    if (increasing)
    {
        m_progressValue += 5;
        if (m_progressValue >= 80)
        {
            increasing = false;
        }
    }
    else
    {
        m_progressValue -= 5;
        if (m_progressValue <= 20)
        {
            increasing = true;
        }
    }

    m_memoryProgress->setValue(m_progressValue);
    m_memoryText->setText(QString("%1%").arg(m_progressValue));

    // 更新电池状态（模拟）
    m_batteryLevel -= 1;
    if (m_batteryLevel <= 0)
    {
        m_batteryLevel = 100;
    }

    m_batteryText->setText(QString("%1%").arg(m_batteryLevel));

    // 根据电量级别更新图标和颜色
    if (m_batteryLevel > 70)
    {
        m_batteryIcon->setAwesome(ElaIconType::BatteryThreeQuarters);
        m_batteryText->setStyleSheet("color: green; font-size: 12px;");
    }
    else if (m_batteryLevel > 30)
    {
        m_batteryIcon->setAwesome(ElaIconType::BatteryQuarter);
        m_batteryText->setStyleSheet("color: orange; font-size: 12px;");
    }
    else
    {
        m_batteryIcon->setAwesome(ElaIconType::BatteryExclamation);
        m_batteryText->setStyleSheet("color: red; font-size: 12px;");
    }
}

void ElaStatusBarTest::toggleThemeIndicator()
{
    m_isLightTheme = !m_isLightTheme;
    updateThemeIndicator();
}

void ElaStatusBarTest::updateThemeIndicator()
{
    if (m_isLightTheme)
    {
        m_themeIcon->setAwesome(ElaIconType::Sun);
        m_themeIcon->setToolTip("当前主题: 浅色模式");
    }
    else
    {
        m_themeIcon->setAwesome(ElaIconType::Moon);
        m_themeIcon->setToolTip("当前主题: 深色模式");
    }
}

void ElaStatusBarTest::showCustomMessage()
{
    QString message = "自定义状态消息 - " + QDateTime::currentDateTime().toString("hh:mm:ss");
    m_customMessage->setText(message);

    // 临时显示消息，3秒后自动清除
    QTimer::singleShot(3000, this, &ElaStatusBarTest::clearCustomMessage);
}

void ElaStatusBarTest::clearCustomMessage()
{
    m_customMessage->setText("");
}